package com.runfast.waimai.web.controller.data;

import com.runfast.common.web.entity.Result;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.waimai.service.RunfastBusinessDailyService;
import com.runfast.waimai.dao.model.RunfastBusinessDailyExample;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecordExample;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import com.runfast.waimai.utils.MyUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by WQ on 2018/6/5.
 */
@RestController
@RequestMapping("/server/data/")
public class BusinessDataController {

    @Autowired
    RunfastBusinessDailyService businessDailyService;
    @Autowired
    RunfastGoodsSellRecordService runfastGoodsSellRecordService;

    @GetMapping("business/list")
    public Result list(Integer id, String date, String businessName, Integer businessId) {
        Validate.notNull(id, "代理商不可为空");
        Validate.notEmpty(date, "日期不可为空");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1;
        try {
            date1 = sdf.parse("date");
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.fail(ResultCode.PARAMETER_ERROR, "日期格式错误");
        }
        RunfastBusinessDailyExample example = new RunfastBusinessDailyExample();
        RunfastBusinessDailyExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id).andDateEqualTo(date1);
        if (StringUtils.isEmpty(businessName)) {
            criteria.andBusinessNameEqualTo(businessName);
        }
        if (businessId != null) {
            criteria.andBusinessIdEqualTo(businessId);
        }
        example.setOrderByClause("date desc,business_id asc");
        return Result.ok("", businessDailyService.selectByExample(example));
    }

    @GetMapping("business/overtime")
    public Result overtime(String date, Integer businessId, @PageableDefault Pageable pageable) {
        Validate.notNull(businessId, "商家不可为空");
        Validate.notEmpty(date, "日期不可为空");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1;
        try {
            date1 = sdf.parse("date");
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.fail(ResultCode.PARAMETER_ERROR, "日期格式错误");
        }
        Date date2 = MyUtil.getEndDay(date1);
        RunfastGoodsSellRecordExample example = new RunfastGoodsSellRecordExample();
        example.createCriteria().andBusinessidEqualTo(businessId).andCreatetimeBetween(date1, date2).andDisTimeLessThanReadyTime();
        example.setOrderByClause("id desc");
        return Result.ok("", runfastGoodsSellRecordService.selectByExampleWithPageable(example, pageable));
    }

    @GetMapping("business/negativeComment")
    public Result negativeComment(String date, Integer businessId, @PageableDefault Pageable pageable) {
        Validate.notNull(businessId, "商家不可为空");
        Validate.notEmpty(date, "日期不可为空");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1;
        try {
            date1 = sdf.parse("date");
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.fail(ResultCode.PARAMETER_ERROR, "日期格式错误");
        }
        Date date2 = MyUtil.getEndDay(date1);
//        return Result.ok("", runfastGoodsSellRecordService.selectNegativeCommentWithPageable(businessId, date1, date2, pageable));
        return null;
    }
}
